﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using MyBind;

public partial class Communicate_Images_BBSListView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataKeyNames = new string[] {"id"};
        if (!IsPostBack)
        {
            if (Request.QueryString["secId"] != null)
            {
                string secId = Request.QueryString["secId"].ToString();
                Session["secId"] = secId;
            }
            bind();
        }
    }

    public void bind()
    {
        if (Session["secId"] != null)
        {
            //默认视图
            string secId = Session["secId"].ToString();
            DAL.bbsTopic dalTop = new bbsTopic();
            DataTable dtTop= dalTop.GetDetailedList(" t.secId=" + secId).Tables[0];
            this.GridView1.DataSource = dtTop.DefaultView;
            GridView1.DataBind();
            //说明
            DAL.bbsSection dal = new DAL.bbsSection(); ;
            DataTable dt = dal.GetList("id="+secId).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string secName = "当前版块『"+dt.Rows[0]["name"].ToString()+"』";
                this.lblSec.Text = secName;
            }
            this.lblTopCount.Text="共"+dtTop.Rows.Count.ToString()+"条主题";
            this.lblTip.Text="版主:";
            string strSecMst=getSecMst(secId);
            this.lblSecMst.Text = strSecMst;
            //需处理的数据
            for(int i=0;i<dtTop.Rows.Count;i++)
            {
                //设置 状态 列 显示的图标
                string strFlag=dtTop.Rows[i]["flag"].ToString().Trim();
                int intFlag = 0;
                if (!strFlag.Equals("") || strFlag.Equals(DBNull.Value))
                {
                    intFlag = int.Parse(strFlag);
                }
                string flagUrl = dalTop.getFlag(intFlag);
                ((Image)GridView1.Rows[i].Cells[0].FindControl("imgFlag")).ImageUrl = flagUrl;
                //处理"回复/浏览"列显示的数据
                string replyCount=dtTop.Rows[i]["replyCount"].ToString();
                string clickCount = dtTop.Rows[i]["clickCount"].ToString();
                string attention=attentionRate(replyCount, clickCount);
                ((Label)GridView1.Rows[i].FindControl("lblRate")).Text = attention;
            }
        }
        
    }




    //关注度（回复与点击数格式化显示）
    public string attentionRate(string replyCount, string clickCount)
    {
        string returnStr = "";
        if (replyCount.Equals("") || replyCount.Equals(DBNull.Value))
        {
            returnStr = "0";
        }
        else
        {
            returnStr = replyCount;
        }
        returnStr += "/";
        if (clickCount.Equals("") || clickCount.Equals(DBNull.Value))
        {
            returnStr += "0";
        }
        else
        {
            returnStr += clickCount;
        }
        return returnStr;
    }
   

    //获得版主信息
    protected string getSecMst(string secId)
    {
        string retStr = "";
        DAL.bbsSecMaster master = new bbsSecMaster();
        DataTable dt = master.GetDetailedList("secId=" + secId).Tables[0];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                retStr += dt.Rows[i]["name"].ToString() + ";";
            }
        }
        else
        {
            retStr = "[暂无]";
        }
        return retStr;
    }

    protected void imbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("BBSIndex.aspx");
    }

    //管理员，或者该版版主,发贴人
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        string attriValue = "javascript:return confirm('确定删除吗？')";
        //        ((ImageButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", attriValue);
        //    }
        //}
    }

    //发帖按钮
    protected void imbAddTop_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["secId"] != null)
        {
            Response.Redirect("BBSAddTopic.aspx");
        }
    }
    //修改
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (Session["secId"] != null)
        {
            string topId = GridView1.DataKeys[e.NewEditIndex].Value.ToString();//
            
            Response.Redirect("BBSAddTopic.aspx?topId=" + topId);
        }
    }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["secId"] != null)
        {
            DAL.bbsTopic dalTop = new DAL.bbsTopic();
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            if (dalTop.Delete(id) > 0)
            {
                WriteScript.Mesg(this.Response, "删除成功！");
                Response.Redirect("BBSListView.aspx");
            }
        }
    }

}
